package com.bsh.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.bsh.dto.CloudGoodsListQryDto;
import com.bsh.mybatis.dataView.CloudGoodsInfo;
import com.bsh.mybatis.model.CloudGoods;
import com.bsh.mybatis.model.Merchant;
import com.bsh.repository.DaoCloudGoods;
import com.bsh.repository.DaoMerchant;
import com.bsh.service.CloudGoodsService;
import com.bsh.utils.Pager;

/**
 * 积分商品
 * 
 * @author Administrator
 *
 */
@Service
@Transactional
public class CloudGoodsServiceImpl implements CloudGoodsService {
	@Autowired
	DaoCloudGoods daoGoods;
	@Autowired
	DaoMerchant daoMerch;

	@Override
	public List<Map<String, Object>> getPage(Pager pager, CloudGoodsListQryDto qryParam) {
		StringBuilder sb = new StringBuilder();
		if (!StringUtils.isEmpty(qryParam.getMerchantName())){
			String mName=qryParam.getMerchantName().trim();		
			sb.append(String.format(" and(m.name = '%s' or m.merchant_name like '%%%s%%')", mName,mName));
		}
		if (!StringUtils.isEmpty(qryParam.getName()))
			sb.append(String.format(" and g.name like '%%%s%%'", qryParam.getName().trim()));
		if (!StringUtils.isEmpty(qryParam.getInStore())&&qryParam.getInStore()!=-1)
			sb.append(String.format(" and g.in_store = %s", qryParam.getInStore()));		
		List<Map<String, Object>> list = daoGoods.getPage(pager, sb.toString());
		return list;
	}

	/**
	 * 添加商品
	 * 
	 * @param goods
	 * @return
	 */
	@Override
	public Integer addGoods(CloudGoods goods, String merchantName) {
		if (!StringUtils.isEmpty(merchantName)) {
			Merchant merch = daoMerch.getByName(merchantName);
			if (merch == null) {
				return -1;// 商户名不存在
			} else {
				goods.setMerchantId(merch.getId());
			}
		}
		
		if (goods.getInStore() == 1)
			goods.setStoreTime(new Date());
		
		
		int res = daoGoods.insert(goods);
		
		return goods.getId();
	}

	/**
	 * 修改商品
	 * 
	 * @param goods
	 * @return
	 */
	@Override
	public int editGoods(CloudGoods goods, String merchantName) {// boolean updateImage

		if (!StringUtils.isEmpty(merchantName)) {
			Merchant merch = daoMerch.getByName(merchantName);
			if (merch == null) {
				return -1;// 商户名不存在
			} else {
				goods.setMerchantId(merch.getId());
			}
		}

		if (goods.getInStore() == 1)
			goods.setStoreTime(new Date());


		CloudGoods gd = daoGoods.selectByPrimaryKey(goods.getId());
		if(StringUtils.isEmpty(goods.getImgBig())){
			goods.setImgBig(gd.getImgBig());
		}
		if(StringUtils.isEmpty(goods.getImgSmall())){
			goods.setImgSmall(gd.getImgSmall());
		}
		return daoGoods.updateGoods(goods);
//		if (updateImage)
//			return daoGoods.updateGoods(goods);
//		else {
//			return daoGoods.updateGoodsExcludeImg(goods);
//		}
	}

	/**
	 * 查询商品
	 */
	@Override
	public CloudGoodsInfo getGoods(Integer id) {
		return daoGoods.getGoods(id);
	}

	/**
	 * 上架\下架
	 * 
	 * @param id
	 * @param isEnabele
	 * @return
	 */
	@Override
	public Integer enable(int id, boolean isEnabele) {
		return daoGoods.enable(id, isEnabele ? 1 : 0);
	}
}
